package com.ts.api.module.api.service.impl;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.module.api.service.ApiPlanRatingService;
import com.ts.api.module.channel.entity.po.ChannelUserProductMatchPO;
import com.ts.api.module.channel.entity.po.ChannelUserRegisterPO;
import com.ts.api.module.channel.entity.qo.ChannelUserQO;
import com.ts.api.module.channel.service.ChannelUserService;
import com.ts.api.module.product.entity.po.ProductCallbackRatingPO;
import com.ts.api.module.product.service.ProductCallbackRatingService;
import com.ts.api.module.product.service.ProductUserMatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

@Service
public class ApiPlanRatingServiceImpl implements ApiPlanRatingService {

    @Autowired
    private ChannelUserService channelUserService;
    @Autowired
    private ProductUserMatchService productUserMatchService;
    @Autowired
    private ProductCallbackRatingService productCallbackRatingService;

    @Override
    public void ratingCallback(Long productId, Long productPlanId, String phoneMd5, String rating) {
        ChannelUserQO channelUserQO = ChannelUserQO.createPhoneMd5(phoneMd5);
        ChannelUserRegisterPO userRegisterPO = channelUserService.get(channelUserQO);
        if (ValidParam.isNull(userRegisterPO)) {
            throw new IllegalArgumentException("非法用户");
        }

        Long userId = userRegisterPO.getId();
        List<ChannelUserProductMatchPO> list = productUserMatchService.listProductMatchByUserId(userId);
        if (ValidParam.isEmpty(list)) {
            throw new IllegalArgumentException("非法回传空记录");
        }

        List<ChannelUserProductMatchPO> listFindMatch = CollUtils.find(list, t -> Eq.object(t.getLoanUserId(), userId)
                && Eq.object(t.getLoanProductId(), productId) && Eq.object(t.getLoanProductPlanId(), productPlanId));
        if (ValidParam.isEmpty(listFindMatch)) {
            throw new IllegalArgumentException("非法回传记录");
        }

        //  更新评分
        ChannelUserRegisterPO updateUserRegisterPO = new ChannelUserRegisterPO();
        updateUserRegisterPO.setId(userId);
        updateUserRegisterPO.setRating(rating);
        channelUserService.update(updateUserRegisterPO);


        //  最新一条记录获取
        listFindMatch.sort(Comparator.comparing(ChannelUserProductMatchPO::getCreateTime, DateUtils::dateTimeCompareTo));
        ChannelUserProductMatchPO firstMatch = listFindMatch.getFirst();

        //  记录主体计划回传信息
        ProductCallbackRatingPO productCallbackRatingPO = new ProductCallbackRatingPO();
        productCallbackRatingPO.setLoanChannelId(firstMatch.getLoanChannelId());
        productCallbackRatingPO.setLoanProductId(firstMatch.getLoanProductId());
        productCallbackRatingPO.setLoanProductPlanId(firstMatch.getLoanProductPlanId());
        productCallbackRatingPO.setLoanUserId(userId);
        productCallbackRatingPO.setPhoneMd5(phoneMd5);
        productCallbackRatingPO.setRating(rating);
        productCallbackRatingPO.setCreateTime(DateUtils.currentTime());
        productCallbackRatingService.insertReplace(productCallbackRatingPO);
    }
}
